package com.zenitera.bigdata.state;

import org.apache.flink.api.common.state.BroadcastState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.ReadOnlyBroadcastState;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.BroadcastConnectedStream;
import org.apache.flink.streaming.api.datastream.BroadcastStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction;
import org.apache.flink.util.Collector;

/**
 * 广播状态（Broadcast state）
 * getBroadcastState
 */
public class Flink01_State_Operator_BroadCast {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);

        env.enableCheckpointing(2000);

        DataStreamSource<String> dataStream = env.socketTextStream("localhost", 6666);
        DataStreamSource<String> configStream = env.socketTextStream("localhost", 7777);

        MapStateDescriptor<String, String> bcStateDesc = new MapStateDescriptor<>("bcState", String.class, String.class);
        BroadcastStream<String> bcStream = configStream.broadcast(bcStateDesc);

        BroadcastConnectedStream<String, String> connectedStream = dataStream.connect(bcStream);

        connectedStream
                .process(new BroadcastProcessFunction<String, String, String>() {
                    @Override
                    public void processElement(String value,
                                               ReadOnlyContext ctx,
                                               Collector<String> out) throws Exception {

                        ReadOnlyBroadcastState<String, String> state = ctx.getBroadcastState(bcStateDesc);

                        String conf = state.get("switch");

                        if ("start".equals(conf)) {
                            out.collect(value + " 启动");
                        } else if ("stop".equals(conf)) {
                            out.collect(value + " 停止");
                        } else if ("restart".equals(conf)) {
                            out.collect(value + " 重启");
                        } else {
                            out.collect(value + " 默认info");
                        }
                    }

                    @Override
                    public void processBroadcastElement(String value,
                                                        Context ctx,
                                                        Collector<String> out) throws Exception {
                        BroadcastState<String, String> state = ctx.getBroadcastState(bcStateDesc);
                        state.put("switch", value);
                    }
                })
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
